package ext.tianma.change.processors;

import java.rmi.RemoteException;

import wt.change2.WTChangeOrder2;
import wt.part.WTPart;
import wt.util.WTException;
import ext.com.iba.IBAUtil;

public class ModifiedFinishedGoodUtil {
	private static final String FINISHEDGOOD = "modifiedFinishedGood";
	
	/**
	 * 获取相应的字符串
	 * @param part
	 * @return
	 */
	public static String getPartInfo( WTPart part ){
		
		String ibaValue = "";
		String number = "";
		String view = "";
		String version = "";
		
		if( part != null ){
			number = part.getNumber();
			view = part.getViewName();
			version = part.getVersionIdentifier().getValue();
		}
		
		ibaValue = number + "," + view + "," + version + ";";
		
		return ibaValue;
		
	}
	
	/**
	 * 重新设置IBA属性
	 * @param ecn
	 * @param value
	 */
	public static void updateIBAValue( WTChangeOrder2 ecn ,String value ){
		String ibaValue = "";
		if( ( ecn != null ) && ( value != null ) ){
			
			try {
				
				String ibaFinishedGood = (String) IBAUtil.getIBAValue( ecn , FINISHEDGOOD );
				if( ibaFinishedGood == null ){
					ibaFinishedGood = "" ;
				}
				
				if( ( !ibaFinishedGood.contains( value ) ) ){
					ibaValue = ibaFinishedGood + value ;
					IBAUtil.forceSetIBAValue(ecn, FINISHEDGOOD, ibaValue);						
				}
				
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * 截取字符串
	 * @param value
	 * @return
	 */
	public static String getValue( String value ){
		String oid = "";
		if( ( value != null ) && ( value.contains( "VR:wt.part.WTPart:" ) ) ){
				
				int lasetIndex = value.lastIndexOf( "$" )+1;
				int lasetIndexv = value.lastIndexOf( "!*" );
				
				if( lasetIndexv > lasetIndex ){
					oid = ( value.substring( lasetIndex , lasetIndexv ) );
				}		
		}
		return oid;
	}
	
	/**
	 * 删除对象重构IBA属性
	 * @param part
	 * @param value
	 * @throws WTException 
	 * @throws RemoteException 
	 */
	public static String getDelectIBAValue( WTPart part , String ibaValue ) throws RemoteException, WTException{
		
		String value ="";
		
		value = getPartInfo( part );
		System.out.println( "Debug   value:"+value );
		if( ibaValue.contains( value ) ){
			System.out.println( "Debug    contains..." );
			ibaValue = ibaValue.replaceAll(value,"");
		}
		System.out.println( "Debug     ibaValue:"+ibaValue );
		return ibaValue;
		
	}
}
